<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ServiceController
 *
 * @author nmthong
 */
class AjaxController extends Controller{
    // Members
    /**
     * Key which has to be in HTTP USERNAME and PASSWORD headers 
     */
    Const APPLICATION_ID = 'ASCCPE';
 
    /**
     * Default response format
     * either 'json' or 'xml'
     */
    private $format = 'json';
    
    /**
     * @return array action filters
     */    

        public function actionGetCategory(){
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="SELECT *,os_category.LinkImage as link FROM os_category
                    WHERE os_category.sale_id =".Yii::app()->session["saleId"];
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_category = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_category[]=$row;
                }
//                if (empty($rows_category)) {
//                    $rows_category = NULL;
//                }
                $string=json_encode($rows_category);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);

             }            
        }
        
        public function actionInsertOrder()
        {
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $current_time=time();
                if(strcmp($json->discount_code,"")!=0){
                    $sql="SELECT discount_percent FROM os_discount WHERE discount_code=".$json->discount_code." and discount_start<".$current_time." and discount_end>".$current_time;
                    $command=$connection->createCommand($sql);
                    $dataReader=$command->query();
                    if(($row=$dataReader->read())!==false) {
                        $json->discount=$json->sum_price*(intval($row)/100);
                    }               
                }
                $sql="CALL SP_INS_ORDER(:name_order,:name_customer,:mobile_customer,:date_order,:discount,:sum_price,:description,:is_finish,@out)";                 
           
                $command=$connection->createCommand($sql);
                $command->bindParam(':name_order',$json->name_order,PDO::PARAM_STR);
                $command->bindParam(':name_customer',$json->name_customer,PDO::PARAM_STR);
                $command->bindParam(':mobile_customer',$json->mobile_customer,PDO::PARAM_STR);
                $command->bindParam(':date_order',$json->date_order,PDO::PARAM_STR);

                $command->bindParam(':discount',$json->discount,PDO::PARAM_STR);                
                $command->bindParam(':sum_price',$json->sum_price,PDO::PARAM_STR);
                $command->bindParam(':description',$json->description,PDO::PARAM_STR);
                $command->bindParam(':is_finish',$json->is_finish,PDO::PARAM_INT);
                $row_count=$command->execute();
                $data = $connection->createCommand("select @out as result;")->queryScalar();
                $json->order_id=$data + 0;
                //insert  items
                $array_orderitems=$json->array_items;
                foreach ($array_orderitems as &$value) {
                    $sql="CALL SP_INS_ORDERITEM(:order_id,:item_id,:price,:name_item,:number_item,:discount)";                 
                    $command=$connection->createCommand($sql);
                    $command->bindParam(":order_id",$data,PDO::PARAM_STR);
                    $command->bindParam(":item_id",$value->Id,PDO::PARAM_INT);
                    $command->bindParam(":price",$value->Price,PDO::PARAM_STR);
                    $command->bindParam(":name_item",$value->NameItem,PDO::PARAM_STR);
                    $command->bindParam(":number_item",$value->NumberOrder,PDO::PARAM_INT);
                    $command->bindParam(":discount",$value->Discount,PDO::PARAM_STR);                
                    $row_count=$command->execute();            
                }
                $resul=array('data'=>$json,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $resul=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($resul);
            }
        } 
        /*
        //old version
         public function actionInsertCategory(){
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
//                $query="INSERT INTO os_category(Id,NameCategory,LinkImage) VALUE('{$_POST['Id']}','{$_POST['NameCategory']}','{$_POST['LinkImage']}')";
//                $connection->createCommand($query)->query();   
                $query="Insert Into os_category(Namecategory,LinkImage,sale_id) VALUES(:NameCategory,:LinkImage,:sale_id)"; 
                $command=$connection->createCommand($query);
//                $command->bindParam(":Id",$_POST['Id'],PDO::PARAM_INT);
                $command->bindParam(":NameCategory",$_POST['NameCategory'],PDO::PARAM_STR);
                $link_image=$_POST['link'];
                $part = explode ('/', $link_image);
                $command->bindParam(":LinkImage",$part[count($part)-1],PDO::PARAM_STR);
                $command->bindParam(":sale_id",Yii::app()->session['saleId'],  PDO::PARAM_INT);
                $row_count=$command->execute();
                $resul=array('data'=>$json,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
             }            
        }
        public function actionEditCategory(){
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
//                $idvalue = Yii::app()->session['idcategory'];
//                $query="UPDATE os_category SET os_category.NameCategory = '{$_POST['NameCategory']}', os_category.LinkImage = '{$_POST['LinkImage']}' WHERE os_category.Id = '{$_POST['Id']}'";
//                $connection->createCommand($query)->query();   
                //get id vallue
                $query_tmp = "select * from os_category";
                $command_tmp=$connection->createCommand($query_tmp);
                $dataReader=$command_tmp->query();
                $rows_category = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_category[]=$row;
                }
                //find categoryId
                $rowId = $_POST['id']-1;
                $categoryId = $rows_category[$rowId]['Id'];
                
                $query="UPDATE os_category SET os_category.NameCategory=:NameCategory, os_category.LinkImage=:LinkImage WHERE os_category.Id=:id"; 
                $command=$connection->createCommand($query);
                $command->bindParam(":NameCategory",$_POST['NameCategory'],PDO::PARAM_STR);
                $link_image=$_POST['link'];
                $part = explode ('/', $link_image);
                $command->bindParam(":LinkImage",$part[count($part)-1],PDO::PARAM_STR);
//                $command->bindParam(":id",$_POST['id'],PDO::PARAM_INT);
                $command->bindParam(":id",$categoryId,PDO::PARAM_INT);
                $execute = $command->execute();
                $resul=array('data'=>$json,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
             }            
        }
         */
        public function actionDeleteCategory(){
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
               //get id vallue
                $query_tmp = "select * from os_category where os_category.sale_id =".Yii::app()->session["saleId"];
                $command_tmp=$connection->createCommand($query_tmp);
                $dataReader=$command_tmp->query();
                $rows_category = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_category[]=$row;
                }
                //find category ID
                $rowId = $_POST['id']-1;
                $categoryId = $rows_category[$rowId]['Id'];
                
//                $idvallue = Yii::app()->session['idcategory'];
                // delete row category
                $query="DELETE FROM os_category WHERE os_category.Id =".$categoryId;
                $connection->createCommand($query)->query();    
                unset(Yii::app()->session['idcategory']);
                unset(Yii::app()->session['namecategory']);
                $resul=array('data'=>$json,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
             }            
        }
        
        public function actionGetItemsNew(){
            try{             
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection

                $query="SELECT *,os_item.LinkImage as link FROM os_item
                    WHERE os_item.sale_id =".Yii::app()->session['saleId'];
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_item = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_item[]=$row;
                }
                $string=json_encode($rows_item);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
            }
        }
        public function actionDeleteItemNew(){
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query_id="SELECT * FROM os_item WHERE os_item.sale_id =".Yii::app()->session["saleId"];
                $command_id=$connection->createCommand($query_id);
                $dataReader=$command_id->query();
                $rows_item = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_item[]=$row;
                }
                $rowId = $_POST['id']-1;
                $itemId = $rows_item[$rowId]['Id'];
                $query = "DELETE FROM os_item WHERE os_item.Id =".$itemId;
                $connection->createCommand($query)->query();        
                $resul=array('data'=>$json,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
             }            
        }
        /*
        //old version
        public function actionGetItems(){
            try{             
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                //get id vallue
                $query_tmp = "select * from os_category";
                $command_tmp=$connection->createCommand($query_tmp);
                $dataReader_tmp=$command_tmp->query();
                $rows_category = array();
                while(($row=$dataReader_tmp->read())!==false) {
                    $rows_category[]=$row;
                }
                $rowval = $_GET['id']-1;
                $idvallue = $rows_category[$rowval]['Id'];
//                unset(Yii::app()->session['namecategory']);       
//                Yii::app()->session['namecategory'] = $rows_category[$rowval]['NameCategory'];
                unset(Yii::app()->session['idcategory']);
                Yii::app()->session['idcategory'] = $idvallue;
                //get Item
                $query="SELECT *,os_item.LinkImage as link FROM os_item WHERE os_item.IdCategory=".$idvallue;
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_item = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_item[]=$row;
                }
                
                
//                //get NameCategory
//                $q="SELECT * FROM os_category WHERE os_category.Id=".$_GET['id'];
//                $cmd=$connection->createCommand($q);
//                $dataRead=$cmd->query();
//                $rows_data = array();
//                while(($row=$dataRead->read())!==false) {
//                    $rows_data[]=$row;
//                }
//                if (!empty($rows_data[0]))
//                Yii::app()->session['namecategory'] = $rows_data[0]["NameCategory"];
                
                //$data=array("items"=>$rows_item);
                $string=json_encode($rows_item);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
            }
        }   
        //old verion
        public function actionInsertItem(){
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection              
                $idCategory = Yii::app()->session['idcategory'];

//                $query="INSERT INTO os_item(NameItem,IdCategory,Price,Description,Discount,Unit) 
//                    VALUE('{$_POST['NameItem']}','{$idCategory}','{$_POST['Price']}','{$_POST['Description']}','{$_POST['Discount']}','{$_POST['Unit']}')";
//                $connection->createCommand($query)->query();          
                $query="Insert Into os_item(NameItem,IdCategory,Price,Description,LinkImage,Discount,Unit) 
                    VALUES(:NameItem,:IdCategory,:Price,:Description,:LinkImage,:Discount,:Unit )"; 
                $command=$connection->createCommand($query);
                $command->bindParam(":NameItem",$_POST['NameItem'],PDO::PARAM_STR);
                $command->bindParam(":IdCategory",$idCategory,PDO::PARAM_INT);
                $command->bindParam(":Price",$_POST['Price'],PDO::PARAM_STR);
                $command->bindParam(":Description",$_POST['Description'],PDO::PARAM_STR);
                $command->bindParam(":Discount",$_POST['Discount'],PDO::PARAM_STR);
                $command->bindParam(":Unit",$_POST['Unit'],PDO::PARAM_STR);
                $link_image=$_POST['link'];
                $part = explode ('/', $link_image);
                $command->bindParam(":LinkImage",$part[count($part)-1],PDO::PARAM_STR);
                $row_count=$command->execute();
                
                $resul=array('data'=>$json,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
             }            
        }
         //old verion
        public function actionEditItem(){
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $idCategory = Yii::app()->session['idcategory'];
                // get IdItem
                $query_tmp = "select * from os_item where os_item.IdCategory =".$idCategory;
                $command_tmp=$connection->createCommand($query_tmp);
                $dataReader=$command_tmp->query();
                $rows_item = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_item[]=$row;
                }
                $rowval = $_POST['id']-1;
                $idvallue = $rows_item[$rowval]['Id'];
//                $IdItem = Yii::app()->request->getPost('Id');
//                $query="UPDATE os_item 
//                            SET os_item.NameItem = '{$_POST['NameItem']}', os_item.IdCategory = '{$idCategory}', os_item.Price = '{$_POST['Price']}', os_item.Description = '{$_POST['Description']}', os_item.Discount = '{$_POST['Discount']}', os_item.Unit = '{$_POST['Unit']}' 
//                            WHERE os_item.Id =".$IdItem;
//                            //, os_item.LinkImage = '{$_POST['LinkImage']}'
//                $connection->createCommand($query)->query();   
                $query="UPDATE os_item 
                    SET os_item.NameItem=:NameItem, os_item.IdCategory=:IdCategory, os_item.Price=:Price, os_item.Description=:Description, os_item.LinkImage=:LinkImage, os_item.Discount=:Discount, os_item.Unit=:Unit 
                    WHERE os_item.Id=:id"; 
                $command=$connection->createCommand($query);
                $command->bindParam(":NameItem",$_POST['NameItem'],PDO::PARAM_STR);
                $command->bindParam(":IdCategory",$idCategory,PDO::PARAM_INT);
                $command->bindParam(":Price",$_POST['Price'],PDO::PARAM_STR);
                $command->bindParam(":Description",$_POST['Description'],PDO::PARAM_STR);
                $command->bindParam(":Discount",$_POST['Discount'],PDO::PARAM_STR);
                $command->bindParam(":Unit",$_POST['Unit'],PDO::PARAM_STR);
                $link_image=$_POST['link'];
                $part = explode ('/', $link_image);

                $command->bindParam(":LinkImage",$part[count($part)-1],PDO::PARAM_STR);
                $command->bindParam(":id",$idvallue,PDO::PARAM_INT);
                $row_count=$command->execute();
                
                $resul=array('data'=>$json,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
             }            
        }
         //old verion
        public function actionDeleteItem(){
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="SELECT * FROM os_item WHERE os_item.IdCategory=".Yii::app()->session['idcategory'];
                unset(Yii::app()->session['idcategory']);
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_item = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_item[]=$row;
                }
                $rowval = $_POST['id']-1;
                $idvallue = $rows_item[$rowval]['Id'];
                $query_2 = "DELETE FROM os_item WHERE os_item.Id =".$idvallue;
                $connection->createCommand($query_2)->query();        
                $resul=array('data'=>$json,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
             }            
        }
*/
        public function actionAddOrder()
        {
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
//                $current_time=time();
                $sale_id = Yii::app()->session["saleId"];
                //code here
                $timestamp = strtotime($_POST['DateOrder']);
                $query="Insert Into os_order(NameOrder,NameCustomer,MobileCustomer,DateOrder,Discount,SumPrice,Description,IsFinish,sale_id) 
                    VALUES(:NameOrder,:NameCustomer,:MobileCustomer,:DateOrder,:Discount,:SumPrice,:Description,:IsFinish,:sale_id)"; 
                $command=$connection->createCommand($query);
                $command->bindParam(":NameOrder",$_POST['NameOrder'],PDO::PARAM_STR);
                $command->bindParam(":NameCustomer",$_POST['NameCustomer'],PDO::PARAM_STR);
                $command->bindParam(":MobileCustomer",$_POST['MobileCustomer'],PDO::PARAM_STR);
                $command->bindParam(":DateOrder",$timestamp,PDO::PARAM_INT);
                $command->bindParam(":Discount",$_POST['Discount'],PDO::PARAM_STR);
                $command->bindParam(":SumPrice",$_POST['SumPrice'],PDO::PARAM_STR);
                $command->bindParam(":Description",$_POST['Description'],PDO::PARAM_STR);
                $command->bindParam(":IsFinish",$_POST['IsFinish'],PDO::PARAM_INT);
                $command->bindParam(":sale_id",$sale_id,PDO::PARAM_INT);
                $row_count=$command->execute();
                 
                $resul=array('data'=>$json,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $resul=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($resul);
            }
        }
        
public function actionGetOrder() {
    try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="SELECT * FROM os_order
                    WHERE os_order.sale_id =".Yii::app()->session["saleId"];
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_order = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_order[]=$row;
//                    $rows_category[]['DateOrder'] = date('m/d/Y', $row['DateOrder']);
                }
                //timestamp -> date time
                foreach ($rows_order as &$value) {
                    $value['DateOrder'] =  date('m/d/Y', $value['DateOrder']);
                }
                $string=json_encode($rows_order);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);

             } 
}
public function actionEditOrder(){
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                // date time -> timestamp
                $timestamp = strtotime($_POST['DateOrder']);            
                $query="UPDATE os_order as a 
                             SET a.NameOrder = '{$_POST['NameOrder']}', a.NameCustomer = '{$_POST['NameCustomer']}', a.MobileCustomer = '{$_POST['MobileCustomer']}', a.DateOrder = '{$timestamp}', a.Discount = '{$_POST['Discount']}', a.SumPrice = '{$_POST['SumPrice']}', a.Description = '{$_POST['Description']}', a.IsFinish = '{$_POST['IsFinish']}' 
                             WHERE a.Id =".$_POST['Id'];

                $connection->createCommand($query)->query();
                
                $resul=array('data'=>$json,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
             }            
        }
        public function actionDeleteOrder(){
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="SELECT * FROM os_order WHERE os_order.sale_id =".Yii::app()->session["saleId"];
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_Order = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_Order[]=$row;
                }                
                $rowID = $_POST['id'] - 1;
                $idvallue = $rows_Order[$rowID]['Id'];
                
                $query_2 = "DELETE FROM os_order WHERE os_order.Id =".$idvallue;
                $connection->createCommand($query_2)->query();      
                
                $resul=array('data'=>$json,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
             }            
        }
        public function actionGetOrderItems(){
            try{    
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="SELECT * FROM os_order WHERE os_order.sale_id =".Yii::app()->session["saleId"];
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_orderitem = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_orderitem[]=$row;
                }
                $rowID = $_GET['id'] - 1;
                $IdVal = $rows_orderitem[$rowID]['Id'];
                unset(Yii::app()->session['IdOrder']);
                Yii::app()->session['IdOrder'] = $IdVal;
                
                $query_2="SELECT * FROM os_orderitem WHERE os_orderitem.IdOrder=".$IdVal;
                $cmd=$connection->createCommand($query_2);
                $dataRead=$cmd->query();
                $rows_data = array();
                while(($row=$dataRead->read())!==false) {
                    $rows_data[]=$row;
                }
                //$data=array("items"=>$rows_item);
                $string=json_encode($rows_data);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
            }
        }
        public function actionEditOrderItem(){
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="UPDATE os_orderitem as a 
                             SET a.Price = '{$_POST['Price']}', a.NameItem = '{$_POST['NameItem']}', a.NumberItem = '{$_POST['NumberItem']}', a.Discount = '{$_POST['Discount']}' 
                             WHERE a.IdOrder = ".Yii::app()->session['IdOrder']." and a.IdItem = ".$_POST['IdItem'];

                $connection->createCommand($query)->query();
                
                $resul=array('data'=>$json,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
             }            
        }
         public function actionDeleteOrderItem(){
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="select * FROM os_orderitem where os_orderitem.IdOrder = ".Yii::app()->session['IdOrder'];
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_OrderItem = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_OrderItem[]=$row;
                }    
                
                $rowID = $_POST['id'] - 1;
                $iditem = $rows_OrderItem[$rowID]['IdItem'];
                
                $query_2 = "DELETE FROM os_orderitem WHERE os_orderitem.IdItem = ".$iditem." and os_orderitem.IdOrder = ".Yii::app()->session['IdOrder'];
                $connection->createCommand($query_2)->query();      
                
                $resul=array('data'=>$json,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
             }            
        }
        public function actionInsertOrderItem() {
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $IdOrder = Yii::app()->session['IdOrder'];
                
                $query="INSERT INTO os_orderitem(IdOrder,IdItem,Price,NameItem,NumberItem,Discount,sale_id) 
                    VALUE('{$IdOrder}','{$_POST['IdItem']}','{$_POST['Price']}','{$_POST['NameItem']}','{$_POST['NumberItem']}','{$_POST['Discount']}',".Yii::app()->session['saleId'].")";
                $connection->createCommand($query)->query();          
                $resul=array('data'=>$json,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
             }   
        }
        //get item for drop function
         public function actionGetFullItems(){
            try{             
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection

                $query="SELECT os_item.Id,os_item.NameItem, os_item.Price, os_item.LinkImage as link FROM os_item 
                    WHERE os_item.sale_id =".Yii::app()->session["saleId"];
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_item = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_item[]=$row;
                }
                $string=json_encode($rows_item);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
            }
        }
        
        public function actionDropOrderItems() {
            try{             
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection

                $query="SELECT * FROM os_item
                    WHERE os_item.sale_id =".Yii::app()->session["saleId"];
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_item = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_item[]=$row;
                }
                $rowid = $_GET['rowid']-1;
                $IdItem = $rows_item[$rowid]['Id'];
                $NameItem = $rows_item[$rowid]['NameItem'];
                $Price = $rows_item[$rowid]['Price'];
                $Discount = $rows_item[$rowid]['Discount'];
                $IdOrder = Yii::app()->session['IdOrder'];
                $sale_id = Yii::app()->session['saleId'];
                
                $query_2="INSERT INTO os_orderitem(IdOrder,IdItem,Price,NameItem,NumberItem,Discount,sale_id) 
                    VALUE('{$IdOrder}','{$IdItem}','{$Price}','{$NameItem}','1','{$Discount}','{$sale_id}')";
                $connection->createCommand($query_2)->query();
                
                $resul=array('data'=>$json,'status'=>'ok');
                $string=json_encode($resul);
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
            }
        }
        
        public function actionGetStore(){
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="SELECT *,os_store.store_image as link FROM os_store 
                    WHERE os_store.sale_id =".Yii::app()->session["saleId"];
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_store = array();

                while(($row=$dataReader->read())!==false) {
                    $rows_store[]=$row;
                }
                $string=json_encode($rows_store);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
             }            
        }
        
        public function actionDeleteStore(){
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="SELECT * FROM os_store 
                    WHERE os_store.sale_id =".Yii::app()->session["saleId"];
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_store = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_store[]=$row;
                }
                $rowId=$_POST['id'] - 1;
                $storeId = $rows_store[$rowId]['store_id']; 
                $querynew = "DELETE FROM os_store WHERE os_store.store_id='{$storeId}'";
                $newcomnand = $connection->createCommand($querynew)->query();
                
                $resul=array('data'=>$json,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
             }            
        }
        public function actionGetNews() {
    try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="SELECT * FROM os_news
                    WHERE os_news.sale_id =".Yii::app()->session["saleId"];
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_news = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_news[]=$row;
//                    $rows_category[]['DateOrder'] = date('m/d/Y', $row['DateOrder']);
                }
                //timestamp -> date time
                foreach ($rows_news as &$value) {
                    $value['news_time'] =  date('m/d/Y', $value['news_time']);
                }
                $string=json_encode($rows_news);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
             }
        }
        public function actionDeleteNews(){
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="select * FROM os_news
                     WHERE os_news.sale_id =".Yii::app()->session["saleId"];
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_news = array();
                while(($row=$dataReader->read())!==false) {
                    $rows_news[]=$row;
                }
                $rowId=$_POST['id'] - 1;
                $newsId = $rows_news[$rowId]['news_id']; 
                $querynew = "DELETE FROM os_news WHERE os_news.news_id='{$newsId}'";
                $newcomnand = $connection->createCommand($querynew)->query();
                
                $resul=array('data'=>$json,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
             }            
        }
        public function actionGetSalePoint() {
            try {
                $connetion = Yii::app()->db;
                $query = "SELECT * FROM os_salepoint WHERE os_salepoint.id =".Yii::app()->session['saleId'];
//                $query = "SELECT * FROM os_salepoint WHERE id = 1";
                $command = $connetion->createCommand($query);
                $dataReader = $command->query();
                $row_salepoint = array();
                while (($row = $dataReader->read())!==false) {
                    $row_salepoint[] = $row;
                }
                if (empty($row_salepoint[0]['sale_name'])) {
                    $string = '';
                } else {
                $string = json_encode($row_salepoint); }
                echo $string; 
                
        }  catch (Exception $e) {
            $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
            echo $string=json_encode($result);
        }
        
        }
        
        public function actionSearchSalePoint() {
            
        }
        public function actionGetTypeBusiness() {
            $type_businness = new TypeBusinessForm;
            $data = $type_businness->getDatabase();
//            $string = array();
//        foreach ($data as $key => $value) {
//            $key++;
//            $string[$key] = $value['name'];
//        }
        $response ='<select>';
    foreach ($data as $key => $value) {
        $key++;
         $response .= '<option value="'.$key.'">'.$value['name'].'</option>';
    }
    $response .= '</select>';

    echo $response;
//            echo json_encode($string);
        }

}
?>
